﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.Mvc;
using AutoMapper;
using Huirui.Cavan.Application.MainBoundedContext.SystemModule;
using Huirui.Cavan.Core.Extension;
using Huirui.Cavan.Domain.MainBoundedContext.SearchModule;
using Huirui.Cavan.Domain.MainBoundedContext.SystemModule;
using Huirui.Cavan.Domain.Seedwork;
using Huirui.Cavan.Presentation.Seedwork;
using Huirui.Cavan.Presentation.ViewModel.SystemModule;
using Newtonsoft.Json;
using System.Reflection;

namespace Huirui.Cavan.Presentation.Controller.SystemModule
{
    public class EmployeeController : EntityController<Employee, string, EmployeeSearch, IEmployeeViewModel, IEmployeeAppService>, IEmployeeController
    {
        private IDepartmentAppService DepartmentAppService { get; set; }
        private IJobAppService JobAppService { get; set; }
        public override string ControllerName
        {
            get { return ControllerConst.EMPLOYEE; }
        }

        public override bool UseDenied
        {
            get { return false; }
        }

        public EmployeeController()
        {
            this.Model = ModelFactory.CreateModel<IEmployeeViewModel>();
        }

        [ActionName(ActionConst.ADD)]
        [HttpGet]
        [ActionPermissionAttribute(Name = "employee_query")]
        public ActionResult addEmployee()
        {
            IList<Department> departments = this.DepartmentAppService.GetBySearch(SearchFactory.CreateDepartmentSearch(this.Model.MasterModel.CurrentUserModel.CorporationCode, SearchIdentifier.Code.CorporationCode)).ToList();
            foreach (Department item in departments)
            {
                SelectListItem li = new SelectListItem();
                li.Text = item.Name;
                li.Value = item.IDToDisplay;
                li.Selected = (this.Model.Item.IsNotNull() && this.Model.Item.Department.IsNotNull() && this.Model.Item.Department.IDToDisplay == li.Value);
                this.Model.DepartmentList.Add(li);
            }
            IList<Job> jobs = this.JobAppService.GetBySearch(SearchFactory.CreateJobSearch()).ToList();
            foreach (Job j in jobs)
            {
                SelectListItem li = new SelectListItem();
                li.Text = j.Name;
                li.Value = j.IDToDisplay;
                li.Selected = (this.Model.Item.IsNotNull() && this.Model.Item.Department.IsNotNull() && this.Model.Item.Department.IDToDisplay == li.Value);
                this.Model.Jobs.Add(li);
            }
            this.Model.Jobs.Add(new SelectListItem() { Text = "添加...", Value = "-1" });

            return View("~/Views/System/v1/addEmployee.cshtml", this.Model);
        }

        [ActionName(ActionConst.ADD)]
        [HttpPost]
        [ActionPermissionAttribute(Name = "employee_edit")]
        public JsonResult addEmployee(EmployeeViewModel model)
        {
            BoolResult jsonResult = null;
            if (model.NoPrefixId.IsNotEmpty())
            {
                model.Item.Id = SecurityAppService.Corporation.Id + "_" + model.NoPrefixId;
            }
            model.Item.Department = this.DepartmentAppService.GetById(model.Item.Department.Id);
            BoolGenericResult<Employee> result = this.AppService.AddOrUpdate(model.Item);
            jsonResult = new BoolResult(result.IsSuccess, result.Messages);
            return Json(jsonResult, "application/json");
        }

        [ActionName(ActionConst.LIST)]
        [ActionPermissionAttribute(Name = "employee_query")]
        public JsonResult ListEmployee()
        {
            EmployeeJsonViewModel json = new EmployeeJsonViewModel();
            json.rows = new List<EmployeeJson>();
            IList<Employee> employees = this.AppService.GetBySearch(SearchFactory.CreateEmployeeSearch(this.Model.MasterModel.CurrentUserModel.CorporationCode, SearchIdentifier.Code.CorporationCode)).ToList();
            foreach (Employee el in employees)
            {
                EmployeeJson ej = Mapper.Map<Employee, EmployeeJson>(el);
                json.rows.Add(ej);
            }
            json.total = employees.Count;

            return Json(json, "application/json", JsonRequestBehavior.AllowGet);
        }

        [ActionName(ActionConst.SEARCH)]
        [HttpPost]
        [ActionPermissionAttribute(Name = "employee_query")]
        public JsonResult GetEmployees(EmployeeViewModel model)
        {
            EmployeeJsonViewModel json = new EmployeeJsonViewModel();
            json.rows = new List<EmployeeJson>();
            IList<Employee> employees = this.AppService.GetBySearch(SearchFactory.CreateEmployeeSearch(model.SearchParam.ID, model.SearchParam.Name)).ToList();
            foreach (Employee el in employees)
            {
                EmployeeJson ej = Mapper.Map<Employee, EmployeeJson>(el);
                json.rows.Add(ej);
            }
            json.total = employees.Count;

            return Json(json, "application/json", JsonRequestBehavior.AllowGet);
        }

        [ActionName(ActionConst.SEARCH)]
        [HttpGet]
        [ActionPermissionAttribute(Name = "employee_query")]
        public ActionResult GetEmployees()
        {
            this.Model.Purpose = RouteData.Values["purpose"].ToString();
            return PartialView("~/Views/System/v1/partial/selectEmployeePartial.cshtml", this.Model);
        }

        [ActionName(ActionConst.DELETE)]
        [HttpPost]
        [ActionPermissionAttribute(Name = "employee_edit")]
        public JsonResult DeleteEmployee(string selected)
        {
            BoolGenericResult<Employee> result = null;
            List<EmployeeJson> employees = JsonConvert.DeserializeObject<List<EmployeeJson>>(selected);

            foreach (EmployeeJson json in employees)
            {
                Employee e = this.AppService.GetById(json.Id);
                result = this.AppService.Delete(e);
            }

            BoolResult jsonResult = new BoolResult(result.IsSuccess, result.Messages);

            return Json(jsonResult, "application/json");
        }
    }
}
